package com.cws4cloud.cws.mall.file.biz.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cws4cloud.cws.common.core.util.R;
import com.cws4cloud.cws.common.log.annotation.SysLog;
import com.cws4cloud.cws.common.oss.props.OssProperties;
import com.cws4cloud.cws.mall.file.api.entity.Config;
import com.cws4cloud.cws.mall.file.biz.service.ConfigService;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;


/**
 * 配置表
 *
 * @author zhulj
 * @date 2021-01-28 11:29:25
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/config" )
@Api(value = "config", tags = "配置表管理")
public class ConfigController {

    private final  ConfigService configService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param config 配置表
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page" )
    @PreAuthorize("@pms.hasPermission('sys_config_get')" )
    public R getConfigPage(Page page, Config config) {
        return R.ok(configService.page(page, Wrappers.query(config)));
    }


    /**
     * 通过id查询配置表
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('sys_config_get')" )
    public R getById(@PathVariable("id" ) Integer id) {
        return R.ok(configService.getById(id));
    }

    /**
     * 新增配置表
     * @param config 配置表
     * @return R
     */
    @ApiOperation(value = "新增配置表", notes = "新增配置表")
    @SysLog("新增配置表" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('sys_config_add')" )
    public R save(@RequestBody Config config) {
        return R.ok(configService.save(config));
    }

    /**
     * 修改配置表
     * @param config 配置表
     * @return R
     */
    @ApiOperation(value = "修改配置表", notes = "修改配置表")
    @SysLog("修改配置表" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('sys_config_edit')" )
    public R updateById(@RequestBody Config config) {
        return R.ok(configService.updateById(config));
    }

    /**
     * 通过id删除配置表
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id删除配置表", notes = "通过id删除配置表")
    @SysLog("通过id删除配置表" )
    @DeleteMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('sys_config_del')" )
    public R removeById(@PathVariable Integer id) {
        return R.ok(configService.removeById(id));
    }


	/**
	 * 查询OSS配置
	 * @param code　代码
	 * @return Result
	 */
	@SysLog(value = "查询OSS配置")
	@GetMapping("/get-config-by-code")
	@ApiOperation(value = "查询OSS配置", notes = "查询OSS配置")
	public R getConfigByCode(@RequestParam String code) {
		return R.ok(configService.getConfigByCode(code));
	}

	/**
	 * 默认配置
	 * @return Result
	 */
	@SysLog(value = "默认配置")
	@ApiOperation(value = "默认配置", notes = "默认配置")
	@GetMapping("/default-oss")
	public R defaultOss() {
		return R.ok(configService.defaultOss());
	}

	/**
	 * 保存默认配置
	 * @param code code
	 * @return Result
	 */
	@SysLog(value = "保存默认配置")
	@ApiOperation(value = "保存默认配置", notes = "保存默认配置")
	@PostMapping("/save-default-oss")
	public R saveDefaultOss(@RequestParam String code) {
		return R.ok(configService.saveDefaultOss(code));
	}

	/**
	 * 保存OSS配置
	 * @param ossProperties　oss配置
	 * @param code
	 * @return
	 */
	@SysLog(value = "保存OSS配置")
	@ApiOperation(value = "保存OSS配置", notes = "保存OSS配置")
	@PostMapping("/save-config-oss")
	public R saveConfigOss(@Valid @RequestBody OssProperties ossProperties, @RequestParam String code) {
		return R.ok(configService.saveConfigOss(ossProperties, code));
	}
}
